const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');

module.exports = {
  entry: {
    // 重新构建client.js
    'vscode-layout-demo/js/client': {
      import: './src/libs/client/client-entry.js',
      library: {
        name: 'McpClient',
        type: 'umd'
      }
    },
    // 构建vscode-layout-demo的JavaScript文件
    'vscode-layout-demo/js/app': path.join(__dirname, 'examples', 'vscode-layout-demo', 'js', 'app.js'),
    'vscode-layout-demo/js/connection': path.join(__dirname, 'examples', 'vscode-layout-demo', 'js', 'connection.js'),
    'vscode-layout-demo/js/dom': path.join(__dirname, 'examples', 'vscode-layout-demo', 'js', 'dom.js'),
    'vscode-layout-demo/js/logger': path.join(__dirname, 'examples', 'vscode-layout-demo', 'js', 'logger.js'),
    'vscode-layout-demo/js/requests': path.join(__dirname, 'examples', 'vscode-layout-demo', 'js', 'requests.js'),
    'vscode-layout-demo/js/ui': path.join(__dirname, 'examples', 'vscode-layout-demo', 'js', 'ui.js')
  },
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'electron-dist'),
    clean: false
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
        },
      },
    ],
  },
  resolve: {
    extensions: ['.js'],
    fallback: {
      stream: false,
      crypto: false,
      buffer: false,
      fs: false,
      path: false,
      os: false,
    },
  },
  externals: {},
  target: 'web',
  node: {
    global: true,
    __filename: false,
    __dirname: false
  },
  plugins: [
    // 复制HTML和CSS文件
    new CopyPlugin({
      patterns: [
        {
          from: path.join(__dirname, 'electron', 'vscode-layout-demo', 'index.html'),
          to: path.join(__dirname, 'electron-dist', 'vscode-layout-demo', 'index.html')
        },
        {
          from: path.join(__dirname, 'electron', 'package.json'),
          to: path.join(__dirname, 'electron-dist', 'package.json')
        },
        {
          from: path.join(__dirname, 'examples', 'vscode-layout-demo', 'css'),
          to: path.join(__dirname, 'electron-dist', 'vscode-layout-demo', 'css')
        }
      ]
    })
  ],
  mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
  optimization: {
    minimize: true
  }
};